//
//  HWGameCircleFooter.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/12/30.
//

import UIKit
import Stevia

class HWGameCircleFooter: UICollectionReusableView {
    
    var bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale, corners: [.layerMinXMaxYCorner,.layerMaxXMaxYCorner]).end()
    
    private let browseImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_look").end()
    private let commitImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("WuKong_commit").end()
    private let likeImageView = c_make_ex(UIButton.self)
        .image("WuKong_like").image("Detail_like_sel", state: .selected).end()
    private let browseLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).text("999浏览").end()
    private let commitLikeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).alignment(.right).end()
    
    var zanButtonClick:((_:String)->())?
    var jumpToDetail:((_:HWGameCircleModel)->())?
    var model:HWGameCircleModel?{
        didSet{
            guard let md = model else { return }
            browseLabel.text = md.look + "浏览"
            commitLikeLabel.text = md.pinglun + "评论 · " + md.zan + "点赞"
            likeImageView.isSelected = md.is_zan
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCircleFooter {
    private func initUI() -> Void {
        backgroundColor = .clear
        
        addSubview(bgView)
        
        bgView.subviews{
            browseImageView
            commitImageView
            likeImageView
            browseLabel
            commitLikeLabel
        }
        
        let tapG = UITapGestureRecognizer.init(target: self, action: #selector(commentButtonClick))
        commitImageView.isUserInteractionEnabled = true
        commitImageView.addGestureRecognizer(tapG)
        likeImageView.addTarget(self, action: #selector(likeButtonClick), for: .touchUpInside)
        let tapBg = UITapGestureRecognizer.init(target: self, action: #selector(tapBg))
        addGestureRecognizer(tapBg)
    }
    
    private func initFrame() -> Void {
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(12*widthScale)
            make.right.equalTo(-12*widthScale)
            make.bottom.top.equalToSuperview()
        }
        
        browseImageView.leading(52*widthScale).top(5*widthScale).size(44*widthScale)
        likeImageView.trailing(4*widthScale).size(44*widthScale)
        likeImageView.CenterY == browseImageView.CenterY
        commitImageView.size(44*widthScale)
        commitImageView.CenterY == likeImageView.CenterY
        commitImageView.Trailing == likeImageView.Leading
        browseLabel.leading(64*widthScale).bottom(15*widthScale)
        browseLabel.Top == browseImageView.Bottom
        commitLikeLabel.trailing(26*widthScale)
        commitLikeLabel.CenterY == browseLabel.CenterY
    }
}

extension HWGameCircleFooter:TTGTextTagCollectionViewDelegate{
    func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {

        
    }
}

extension HWGameCircleFooter {
    @objc func tapBg() -> Void {
        jumpToDetail!(model ?? HWGameCircleModel())
    }
    
    @objc private func commentButtonClick() {
        jumpToDetail!(model ?? HWGameCircleModel())
    }
    
//    @objc private func zanButtonClick() {
//        zanButtonClick!(model?.id ?? "")
//    }
    
    @objc private func likeButtonClick(){
        zanButtonClick!(model?.id ?? "")
    }
    
}
